package com.xiao.activity;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;

import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bokeyuan.R;
import com.xiao.news.listview.SettingAdapter;
import com.xiao.news.listview.SettingInfo;
import com.xiao.slipUtils.D;

public class ActivitySeting extends Activity {
	SharedPreferences mShare;
	SharedPreferences mSharebg,mShareMain;

	ArrayList<SettingInfo> mInfo;
	GridView  mGrid;
	SettingAdapter mAdapter;
	CheckBox mCkDownload,mEye;
	TextView mTvFeedback,mTvAbout;
	LinearLayout mLayout;
	MainActivity main;
	//记住点击的次数
	int i=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seting);
		mSharebg=getSharedPreferences(D.ID_GRID, MODE_PRIVATE);
		//初始化view
		initview();
		//将背景照片存放在gridview里面
		initData();
		//取出护眼模式状态
		getCheckStatus();
		//设置适配器
		setAdapter();
		//设置gridview点击事件
		setGridListener();
		//设置护眼模式等按钮事件
		setListener();
		//得到grildview状态
		getGridViewStatic();
	}
	/**
	 *得到grildview状态 
	 */
	private void getGridViewStatic() {
		// TODO Auto-generated method stub
		int p = mSharebg.getInt(D.ID_GRID, -1);
		if(p!=-1){
			mAdapter.notifyPosition(p);
		}

	}
	private void getCheckStatus() {
		//取出护眼模式状态
		boolean ischeck = mShare.getBoolean(D.HELPEYE, false);
		//设置护眼模式状态
		mEye.setChecked(ischeck);
		if(ischeck){
			mLayout.setBackgroundResource(R.drawable.helpeye);
		}else{
			mLayout.setBackgroundColor(Color.WHITE);
		}

	}
	/**
	 * //设置护眼模式等按钮事件
	 */
	private void setListener() {
		//下载博主头像按钮
		setDownLoadImage();
		//护眼模式按钮
		setProtectEyeListener();
		//反馈按钮
//		setFeedBackListener();
		//设置关于按钮
		setAboutListener();
		// 设置反馈按钮
		feedBack();

	}
	
	
	private void feedBack() {
		findViewById(R.id.as_feedback).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {


				//			
				//				Uri uri=Uri.parse("mailto://2509585556@qq.com");
				//				Intent intent=new Intent(Intent.ACTION_SEND, uri);
				//				intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
				//				intent.putExtra(Intent.EXTRA_TEXT, "hello word");
				//				startActivity(intent);
				//			
				Intent email = new Intent(Intent.ACTION_SEND);
				email.setType("plain/text");
				String emailSubject = "共享软件";
				// 设置邮件默认地址
				email.putExtra(android.content.Intent.EXTRA_EMAIL, "2509585556@qq.com");
				// 设置邮件默认标题
				email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
				String emailBody="helllo";
				// 设置要默认发送的内容
				email.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
				// 调用系统的邮件系统
				startActivityForResult(Intent.createChooser(email, "请选择邮件发送软件"), 1001);
				
				
				/*
				 * 短信发送
				 * Uri smsToUri = Uri.parse("smsto:");
		Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
		 sendIntent.putExtra("address", "18205625020"); // 电话号码，这行去掉的话，默认就没有电话
		sendIntent.putExtra("sms_body", "我要共享这个软件");
		sendIntent.setType("vnd.android-dir/mms-sms");
		startActivityForResult(sendIntent, 1002);
				 */

			}
		});
	}
	/**
	 * //设置关于按钮
	 */
	private void setAboutListener() {
		mTvAbout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder=new AlertDialog.Builder(ActivitySeting.this);
				builder.setTitle("个人信息")
				.setMessage("张效伟").create().show();


			}
		});
	}
	
	
//	/**
//	 * 	//反馈按钮
//	 */
//	private void setFeedBackListener() {
//		mTvFeedback.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				Intent i = new Intent(Intent.ACTION_SEND);
//				i.setType("message/rfc822");
//				i.putExtra(Intent.EXTRA_EMAIL, new String[] { "2509585556@qq.com" });
//				i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
//				i.putExtra(Intent.EXTRA_TEXT, "hello");
//				try {
//					startActivity(Intent.createChooser(i, "Send mail..."));
//				} catch (android.content.ActivityNotFoundException ex) {
//					Toast.makeText(ActivitySeting.this, "2509585556@qq.com",
//							Toast.LENGTH_SHORT).show();
//				}
//			}
//		});
//	}
	/**
	 * //护眼模式按钮
	 */
	private void setProtectEyeListener() {
		final Editor edit=mShare.edit();
		mEye.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isClick) {
				// TODO Auto-generated method stub
				if(isClick){
					mLayout.setBackgroundResource(R.drawable.helpeye);
					edit.remove(D.HELPEYE);
					edit.putBoolean(D.HELPEYE, true);
				}else{
					mLayout.setBackgroundColor(Color.WHITE);
					edit.remove(D.HELPEYE);
					edit.putBoolean(D.HELPEYE, false);
				}
				edit.commit();
			}
		});
	}
	/**
	 * //下载博主头像按钮
	 */
	private void setDownLoadImage() {
		mCkDownload.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub

			}
		});
	}
	/**
	 * //设置gridview点击事件
	 */
	private void setGridListener() {
		//保存在mainactivity里的
		mShareMain=getSharedPreferences(D.ID_IMAGE, MODE_PRIVATE);
		final Editor editmain = mShareMain.edit();
		final Editor edit = mSharebg.edit();

		//记住点击的次数
		final int i=1;
		mGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int postion,
					long arg3) {

				//              mAdapter.mPosition;
				if(i!=1&&(mAdapter.mPosition.size()<=0)){
					//表示此时什么都没选
					editmain.putInt(D.ID_IMAGE, -1);
					//表示此时什么都没选
					edit.putInt(D.ID_GRID, -1);
				}else{

					//取出集合中图片的ID
					int id = mInfo.get(postion).getImageId();
					//将对应的图片ID保存在xml文件中
					editmain.putInt(D.ID_IMAGE, id);
					edit.putInt(D.ID_GRID, postion);

				}
				//提交
				editmain.commit();
				edit.commit();
				//刷新适配器
				mAdapter.notifyPosition(postion);
			}
		});
	}
	/**
	 * //初始化view
	 */
	private void initview() {

		mInfo=new ArrayList<SettingInfo>();
		mGrid=(GridView) findViewById(R.id.as_grid);
		mCkDownload=(CheckBox) findViewById(R.id.as_ck1);
		mEye=(CheckBox) findViewById(R.id.as_ck2);
		mTvFeedback=(TextView) findViewById(R.id.as_feedback);
		mTvAbout=(TextView) findViewById(R.id.as_about);
		mLayout=(LinearLayout) findViewById(R.id.as_main);
	}
	/**
	 * //设置适配器
	 */
	private void setAdapter() {
		// TODO Auto-generated method stub

		mAdapter=new SettingAdapter(mInfo, this);
		mGrid.setAdapter(mAdapter);
	}
	/**
	 * //将背景照片存放在gridview里面
	 */
	private void initData() {
		//创建mainactivity对象  用于设置背景
		main=new MainActivity();
		mShare=getSharedPreferences(D.HELPEYE,MODE_PRIVATE);
		mInfo=new ArrayList<SettingInfo>();
		mGrid=(GridView) findViewById(R.id.as_grid);

		int[] id=new int[]{
				R.drawable.bg01,R.drawable.bg02,
				R.drawable.bg03,R.drawable.bg04,	
				R.drawable.bg05,R.drawable.bg06,	
		};
		for (int i = 0; i < id.length; i++) {
			SettingInfo info=new SettingInfo();
			info.setImageId(id[i]);
			mInfo.add(info);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_seting, menu);
		return true;
	}

}
